/*************************************************************
 * Title: 
 * Description: 
 * Author: Snow Huang
 * Email: Shaobin.Software@gmail.com Shaobin_Software@163.com
 * CreateTime: Aug 20, 2012 7:10:37 AM
 * Copyright © 落羽社区 All right reserved
 ************************************************************/
package snow.core.message;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.util.Assert;

import snow.core.exception.ApplicationException;
import snow.core.toolkit.format.DataFormatter;

/**
 * JMS消息发送器
 */
public class MessageSender implements InitializingBean {

	protected static final String DEFAULT_PROPERTY_NAME = "result";

	private final Log log = LogFactory.getLog(getClass());

	private Destination destination; // 发送目的地

	private JmsTemplate jmsTemplate; // JMS工具类

	private String propertyName; // 消息对应的属性名称

	private DataFormatter dataFormatter; // 格式转化器

	public void afterPropertiesSet() throws Exception {
		Assert.notNull(destination, "The property [destination] must not be null!");
		Assert.notNull(jmsTemplate, "The property [jmsTemplate] must not be null!");
		if (StringUtils.isBlank(propertyName)) {
			propertyName = DEFAULT_PROPERTY_NAME;
		}
	}

	public DataFormatter getDataFormatter() {
		return this.dataFormatter;
	}

	public Destination getDestination() {
		return this.destination;
	}

	public JmsTemplate getJmsTemplate() {
		return this.jmsTemplate;
	}

	public String getPropertyName() {
		return this.propertyName;
	}

	/**
	 * 执行发送
	 * 
	 * @param object
	 */
	public void send(final Object object) {
		// 发送到JMS服务器
		jmsTemplate.send(destination, new MessageCreator() {
			/**
			 * 创建消息
			 * 
			 * @param session
			 * @return
			 * @throws JMSException
			 * @see org.springframework.jms.core.MessageCreator#createMessage(javax.jms.Session)
			 */
			public Message createMessage(Session session) throws JMSException {
				final Message message = session.createMessage();
				if (dataFormatter != null) {
					String format = StringUtils.EMPTY;
					try {
						log.debug("format data for MessageSender -> start execution");
						format = dataFormatter.format(object);
						log.debug("the result of format -> " + format);
					} catch (ApplicationException e) {
						log.warn(e.getMessage(), e);
					} finally {
						log.debug("format data for MessageSender -> the end of execution");
					}
					message.setStringProperty(propertyName, format);
				} else {
					message.setObjectProperty(propertyName, object);
				}
				return message;
			}
		});
	}

	public void setDataFormatter(DataFormatter dataFormatter) {
		this.dataFormatter = dataFormatter;
	}

	public void setDestination(Destination destination) {
		this.destination = destination;
	}

	public void setJmsTemplate(JmsTemplate jmsTemplate) {
		this.jmsTemplate = jmsTemplate;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}
}
